import axios from 'axios'
import { setToken } from '~/utils/auth'
import router from '../.nuxt/router'
import store from '../.nuxt/store'
import { LOADING } from '../store/mutation-types/loading/index';
import { getLastParam } from '../utils/auth';
import qs from 'qs';

import { Message } from 'element-ui'
//添加请求拦截器

if (process.BROWSER_BUILD) {
    axios.interceptors.request.use(function(config) {
        //在发送请求之前做某事
        store.commit(LOADING, {
            loading: true
        });
        config.timeout = '30000';
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        if (config.url == process.env.baseUrl + '/member/photoUpload') {
            config.headers['Content-Type'] = 'multipart/form-data';
        }
        if (config.url == process.env.baseUrl + "/b2c/photo/upload.do") {
            config.headers['Content-Type'] = 'multipart/form-data';
        }
		if (config.url == process.env.baseUrl + '/admin/shop/information/photoUpload') {
            config.headers['Content-Type'] = 'multipart/form-data';
        }
        if (config.url == process.env.baseUrl + '/keywords/importKeywords') {
            config.headers['Content-Type'] = 'multipart/form-data';
        }
        if (config.url == process.env.baseUrl + '/agency/importAgency') {
            config.headers['Content-Type'] = 'multipart/form-data';
        }
        
        if (config.url != process.env.baseUrl + '/login') { //如果是login页面就不需要检测token
            if (process.BROWSER_BUILD) {
                var token = window.localStorage.getItem('token');
                if (token) {
                    var method = config.method;
                    switch (method) {
                    case 'get':
                        var params = config.params || {};
                        params['token'] = token;
                        break;
                    case 'post':
                        var data = config.data || {};
                        data['token'] = token;
                        if (config.headers['Content-Type'] != 'multipart/form-data') {
                            config.transformRequest = [function(data) {
                                data = qs.stringify(data);
                                return data;
                            }];
                        }
                        break;
                    }
                } else {
                    router.replace({
                        path: '/login'
                    })
                }
            }
        }
        return config;
    }, function(error) {
        store.commit(LOADING, {
            loading: false
        });
        return Promise.reject(error);
    });

    //添加响应拦截器
    axios.interceptors.response.use(function(response) {
        let specialUrl = getLastParam(response.config.url).split('?')[0];

        if (specialUrl == '/downLoad') { //特殊情况：下载
            return response;
        }

        store.commit(LOADING, {loading: false});

        if (!response.data.successed) { //错误
            if (response.data.message) {
                Message.error({
                    message: response.data.message
                });
            } else {
                Message.error({
                    message: '系统错误'
                });
            }
            return false;
        } else {
            return response;
        }

    }, function(error) {
        store.commit(LOADING, {
            loading: false
        });

        var url = error.response.config.url;
        if (url != process.env.baseUrl + '/login' && error.response) { //防止登录错误时返回的401对tokenExpired造成的影响
            switch (error.response.status) {
            case 401:
                //返回 401 清除token信息并跳转到登录页面
                setToken('token', '');
                setToken('tokenExpired', 'true');
                router.replace({
                    path: '/login'
                });
                break;
            case 404:
            case 500:
            case 502:
                //返回 401 清除token信息并跳转到登录页面
                Message.error({
                    message: '您请求的接口地址不存在'
                });
                break;
            }
        }

        //console.log(error)
        return Promise.reject(error); // 返回接口返回的错误信息
    })
}


export default axios